import { defineStore } from 'pinia'
import * as api from '@/api/org'

export const orgStore = defineStore('orgStore', {
  state: () => ({
    //机构列表
    orgList: [],
  }),
  actions: {
    // 获取机构列表
    async getOrgList() {
      try {
        const res = await api.getOrgList()
        this.orgList = res || []
        return this.orgList
      } catch(error) {
        throw error
      }
    },

    async getOrgOne(id) {
      try {
        const res = await api.getOrgOne(id)
        return res  
      } catch(error) {
        throw error
      }
    },

    async addOrg(dataForm) {
      try {
        const res = await api.addOrg(dataForm)
        return res
      } catch(error) {
        throw error
      }
    },

    async editOrg(dataForm) {
      try {
        const res = await api.editOrg(dataForm)
        return res
      } catch(error) {
        throw error
      }
    },

    // 统一保存方法，新增或修改
    async saveOrg(dataForm) {
      {
        return dataForm.id ?
              this.editOrg(dataForm) :
              this.addOrg(dataForm) 
      }
    }, 

    async deleteOrg(id) {
      try {
        const res = await api.deleteOrg(id)
        // console.log(res)
        return res
      } catch(error) {
        throw error
      }
    }
  }

})